package com.ymh.git.thread.juc.account;

/**
 * Created by aivin on 2018/11/1.
 * 模拟银行或者支付公司的转账的动作
 */
public class PayCompany {

    private static class TransferThread extends Thread {

        private String name;
        private UserAccount from; //转出
        private UserAccount to; //转入
        private int amount; //转账金额
        private ITransfer transfer;

        public TransferThread(String name, UserAccount from, UserAccount to, int amount, ITransfer transfer) {
            this.name = name;
            this.from = from;
            this.to = to;
            this.amount = amount;
            this.transfer = transfer;
        }

        @Override
        public void run() {
            Thread.currentThread().setName(name);
            transfer.transfer(from, to, amount);
        }
    }

    public static void main(String[] args) {
        PayCompany payCompany = new PayCompany();
        UserAccount zhangsan = new UserAccount("张三", 20000);
        UserAccount lisi = new UserAccount("李四", 20000);
//        ITransfer transfer = new TransferAccount(); //线程不安全的转账动作
        ITransfer transfer = new SafeOperate();

        //张三向李四转账2000块钱
        TransferThread zhangsanToLisi = new TransferThread("zhangsanToLisi", zhangsan, lisi, 2000, transfer);

        //李四向张三转账4000块钱
        TransferThread lisiToZhangsan = new TransferThread("lisiToZhangsan", lisi, zhangsan, 4000, transfer);

        zhangsanToLisi.start();
        lisiToZhangsan.start();
    }
}
